package net.easyjava.third.juhe.utils;

import com.alibaba.fastjson.JSONObject;
import net.easyjava.third.juhe.bean.ResultBean;
import net.easyjava.third.juhe.exception.JuheException;
import net.easyjava.tools.http.HttpTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;

/**
 * Created by wxnacy on 16/7/17.
 */
public class ApiUtils {
    private static Logger logger = LoggerFactory.getLogger(ApiUtils.class);



    public static ResultBean send(String url,Map<String,String> params) throws IOException {
        String json = HttpTools.post(url,params);
        ResultBean resultBean = JSONObject.parseObject(json,ResultBean.class);
        logger.info("juhe api result error_code:{} reason:{}", resultBean.getErrorCode(), resultBean.getReason());
        if(resultBean.isError()){
            throw new JuheException("juhe api result error_code: "+resultBean.getErrorCode() + " , reason: " +resultBean.getReason());
        }
        return resultBean;
    }


    public static void main(String[] args) {
        try {
            ApiUtils.send("http://op.juhe.cn/189/bus/busline?dtype=json&city=%E5%8C%97%E4%BA%AC&bus=455&key=0376b00558309f4589a5256562e9b788",null);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
